package com.impetus.project.skilltest.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.impetus.project.skilltest.core.SkillTestException;
import com.impetus.project.skilltest.dataobject.TestTransData;
import com.impetus.project.skilltest.util.HibernateUtil;

public class TestTransDataDAO {
	private static Logger logger = Logger.getLogger(TestTransDataDAO.class.getName());
	
	private static SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
	
	@SuppressWarnings("unchecked")
	public static List<TestTransData> getTestTransDataByTestId ( String testId )
	throws SkillTestException
	{
		List<TestTransData> testTransDataList = new ArrayList<TestTransData>();
		
		try{
			testTransDataList = sessionFactory.getCurrentSession()
			.createCriteria(TestTransData.class)
			.add(Restrictions.like("TESTID", testId)).list();
		}catch(Exception e)
		{
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}
		
		return testTransDataList;
	}
	
	@SuppressWarnings("unchecked")
	public static List<TestTransData> getTestTransDataByCandidateId ( String candidateId )
	throws SkillTestException
	{
		List<TestTransData> testTransDataList = new ArrayList<TestTransData>();
		
		try{
			testTransDataList = sessionFactory.getCurrentSession()
			.createCriteria(TestTransData.class)
			.add(Restrictions.like("CANDIDATEID", candidateId)).list();
		}catch(Exception e)
		{
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}
		
		return testTransDataList;
		
	}
	
	@SuppressWarnings("unchecked")
	public static List<TestTransData> getTestTransDataByTestIdCandidateId ( String testId, String candidateId )
	throws SkillTestException
	{
		List<TestTransData> testTransDataList = new ArrayList<TestTransData>();
		
		try
		{
			testTransDataList = sessionFactory.getCurrentSession()
			.createCriteria(TestTransData.class)
			.add(Restrictions.like("CANDIDATEID", candidateId))
			.add(Restrictions.like("TESTID", testId))
			.list();
		}
		catch(Exception e)
		{
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}
		
		return testTransDataList;		
	}
	
	public static void saveUpdateTestTransData(TestTransData testTransData)
	throws SkillTestException
	{
		try
		{
			sessionFactory.getCurrentSession().saveOrUpdate(testTransData);
		
		}catch(Exception e)
		{
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}
	}	
	
	public static void deleteTestTransData(TestTransData testTransData)
	throws SkillTestException
	{
		try
		{
			sessionFactory.getCurrentSession().delete(testTransData);
			
		}catch(Exception e)
		{
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}
	}
	
	public static String deleteTestTransDataByTestId(String testId)
	throws SkillTestException
	{
		String status = null;
		try
		{
			String hql = "delete from com.impetus.project.skilltest.dataobject.TestTransData transData" +
					" where TESTID = '"+testId+"'";
			
			int rows =	sessionFactory.getCurrentSession()
						.createQuery(hql)
						.executeUpdate();
			
			status = "Deleted ["+rows+"] Test Trans Data records from database.";
			
		}catch(Exception e)
		{
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}
		
		return status;
	}
}
